import {
    reqConsumerDeviceMuffling,
    reqDeviceData,
    reqConsumerAlarmPage,
    reqDoorList
  } from '../../../../request/api.js'
  import {
    showMsg
  } from '../../../../utils/config.js'
  const app = getApp()
  
  Page({
    data: {
      alarmList: [],
      editInfo: {},
      deviceData: {},
      status:0,
    },
  
    onLoad(options) {
      console.log(JSON.parse(options.item), 'options');
      this.setData({
        editInfo: JSON.parse(options.item)
      })
      this.getDeviceData() // imei查询最新设备数据
      this.getConsumerAlarmPage()  // 告警列表
    },
  
    onShow() {
      if(app.globalData.deviceInfo!=null){
        console.log(app.globalData.deviceInfo ,'deviceInfo');
        this.setData({
          "editInfo.deviceName":app.globalData.deviceInfo.deviceName,
          "editInfo.roomId":app.globalData.deviceInfo.roomId,
          "editInfo.roomName":app.globalData.deviceInfo.roomName
        })
        app.globalData.deviceInfo =null
        wx.setStorageSync('deviceInfo',null);
      }
      this.getDataList()
    },
   async getDataList(){
    let params = {
      imei: this.data.editInfo.deviceCode,
  }
      const res = await reqDoorList(1, 1, params)
      if (res.code !== 20000) return showMsg(res.message)
      console.log(res);
      this.setData({
        status:res.data.rows.status||0
      })
    },
    // 历史记录
    onAlarm() {
      // let deviceInfo = JSON.stringify({
      //   imei:this.data.editInfo.deviceCode,
      //   deviceType:this.data.editInfo.deviceType,
      //   flag:true
      // })
      // wx.navigateTo({
      //   url: `/packageA/pages/index-inside-page/alarmRecord/alarmRecord?deviceInfo=${deviceInfo}`,
      // })
      wx.navigateTo({
        url: `/packageC/pages/device/alarmRecord/alarmRecord?deviceCode=${this.data.editInfo.deviceCode}&deviceType=${this.data.editInfo.deviceType}`,
      })
    },
    onInfo(){
      const mergedData = { 
        ...this.data.deviceData, 
        ...this.data.editInfo 
      };
      let deviceData = JSON.stringify(mergedData)
      wx.navigateTo({
        url: `/packageC/pages/device/deviceInfo/deviceInfo?deviceData=${deviceData}`,
      })
    },
    // imei查询最新设备数据
    async getDeviceData() {
      const res = await reqDeviceData(this.data.editInfo.deviceCode)
      if (res.code !== 20000) return showMsg(res.message)
      console.log(res,'imei查询最新设备数据');
      this.setData({
        deviceData: res.data.rows || {}
      })
    },
    
     // 告警列表
      async getConsumerAlarmPage() {
        const res = await reqConsumerAlarmPage(1,50,{
          userId: app.globalData.userId,
          homeId: app.globalData.familyInfo.id,
          deviceCode:this.data.editInfo.deviceCode
        })
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        if(res.data.total==0) return
        // 找到最近的日期的告警数据（最多10条）
        const recentDate = res.data.rows.map(item => item.createTime.split(' ')[0]).reduce((a, b) => (a > b ? a : b)); // 找到最大的日期（最近的日期）
         let recentDate2 = res.data.rows
        .filter(item => item.createTime.startsWith(recentDate))
        .slice(0, 10);
        recentDate2.forEach(item=>{
          item.createTime2=   item.createTime.slice(-8)
        })
        console.log(recentDate2);
  
        this.setData({
          alarmList: recentDate2 || {}
        })
      },
  
  })